package com.osdp.uc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.osdp.common.http.Result;
import com.osdp.common.util.MessageUtil;
import com.osdp.uc.service.IUcPositionService;
import com.osdp.uc.vo.UcPositionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Api(tags = "岗位信息")
@RestController
@RequestMapping("/uc/position")
public class UcPositionController {

    @Autowired
    private IUcPositionService ucPositionService;

    /**
     * 岗位管理分页查询
     * @param positionVo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/pageList", method = RequestMethod.GET)
    public IPage<UcPositionVo> pageList(UcPositionVo positionVo,
                                              @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") int pageSize){
        return ucPositionService.pageListByOrgId(positionVo,new Page<>(pageNo, pageSize));
    }

    /**
     * 用于岗位选择的分页主界面，显示部门名称，职务名称
     * @param positionVo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/selectPositionPage", method = RequestMethod.GET)
    public IPage<UcPositionVo> selectPositionPageList(UcPositionVo positionVo,
                                                @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") int pageSize){
        return ucPositionService.selectPositionPageList(positionVo,new Page<>(pageNo, pageSize));
    }

    /**
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public boolean insert(@RequestBody UcPositionVo positionVo) { //JSONObject jsonObject
//        UcPositionVo positionVo = JSON.parseObject(jsonObject.toJSONString(), UcPositionVo.class);
        return ucPositionService.insertPosition(positionVo);
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public boolean update(@RequestBody JSONObject jsonObject) {
        UcPositionVo positionVo = JSON.parseObject(jsonObject.toJSONString(), UcPositionVo.class);
        return ucPositionService.updatePosition(positionVo);
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result deleteBatch(@RequestParam(name = "ids", required = false) String ids){
        if(StringUtils.isEmpty(ids)) {
            return Result.fail(MessageUtil.getMessage(10051));
        }
        boolean isOk = ucPositionService.deleteBatcah(Arrays.asList(StringUtils.split(ids, ",")));
        return isOk ? Result.success(MessageUtil.getMessage(10049)) : Result.fail(MessageUtil.getMessage(10051));
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除岗位")
    public Result delete(@RequestParam(name = "id", required = true) String id) {
        if(StringUtils.isEmpty(id)) {
            return Result.fail(MessageUtil.getMessage(10051));
        }
        boolean isOk = ucPositionService.deletePosition(id);
        return isOk ? Result.success(MessageUtil.getMessage(10049)) : Result.fail(MessageUtil.getMessage(10051));
    }

    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public UcPositionVo queryById(@RequestParam(name = "id", required = true) String id) {
        return  ucPositionService.getByPositionId(id);
    }
}
